package com.winning.devops.batch.writer.ch02.config;

import com.winning.devops.batch.writer.ch02.domain.Customer;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * JdbcItemWriter配置类
 *
 * @author chensj
 * @date 2020-03-07 15:49
 */
@Configuration
public class JdbcDemoItemWriterConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public JdbcBatchItemWriter<Customer> jdbcDemoItemWriter() {
        JdbcBatchItemWriter<Customer> writer = new JdbcBatchItemWriter<>();
        writer.setDataSource(dataSource);
        writer.setSql("insert into customer (id,firstName,lastName,birthday) " +
                "values (:id,:firstName,:lastName,:birthday)");
        // BeanPropertyItemSqlParameterSourceProvider 会替换上面的占位符
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Customer>());
        writer.afterPropertiesSet();
        return writer;
    }
}
